package cn.wolfcode.web.controller;

import cn.wolfcode.qo.CustomerReportQueryObject;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.Mergeable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String customerReport(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        PageInfo<HashMap> pageInfo = customerReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customerReport/list";
    }

    @RequestMapping("/listByBar")
    public String listByBar(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据,要根据筛选条件
        List<HashMap> maps = customerReportService.listAll(qo);
        //提供一个集合存储x轴数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴数据
        ArrayList yList = new ArrayList();
        //把数据变量遍历放到对应集合中
        for (HashMap map : maps) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }


    @RequestMapping("/listByPie")
    public String listByPie(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据,要根据筛选条件
        List<HashMap> maps = customerReportService.listAll(qo);
        //提供一个集合存储分组类型的数据
        ArrayList groupTypeValues = new ArrayList();
        //提供一个集合存储饼图数据
        ArrayList data = new ArrayList();
        for (HashMap map : maps) {
            groupTypeValues.add(map.get("groupType"));
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("name", map.get("groupType"));
            hashMap.put("value", map.get("number"));
            data.add(hashMap);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("groupTypeValues", JSON.toJSONString(groupTypeValues));
        model.addAttribute("data", JSON.toJSONString(data));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByPie";
    }
}
